package com.cake.liulanxiangzhu.security;

import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.mapper.AdminUserMapper;
import com.cake.liulanxiangzhu.pojo.vo.AdminUserVO;
import com.cake.liulanxiangzhu.utils.DataUseUtil;
import com.cake.liulanxiangzhu.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.cake.liulanxiangzhu.service.impl.AdminUserServiceImpl.LOGINSMS;

@Slf4j
@Service
public class TextMessageServiceImpl implements AuthenticationProvider {
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        log.debug("执行了自定义验证");
        String phone = authentication.getName();
        String codeNum = (String) authentication.getCredentials();
        AdminUserVO adminUserVO = adminUserMapper.selectPermissionByPhone(phone);
        if (adminUserVO == null) {
            String message = "登录失败，用户名不存在！";
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
        String code = (String) redisTemplate.opsForValue().get(LOGINSMS+phone);
        if (DataUseUtil.isNotBlank(code)){
           if (codeNum.equals(code)){
               List<GrantedAuthority> authorities = new ArrayList<>();
               for (String permission : adminUserVO.getPermissions()) {
                   GrantedAuthority authority = new SimpleGrantedAuthority(permission);
                   authorities.add(authority);
               }
               AdminDetails adminDetails = new AdminDetails(adminUserVO.getPhone(),adminUserVO.getPassword(),adminUserVO.getEnable()==1,authorities);
               adminDetails.setId(adminUserVO.getId());
               return new UsernamePasswordAuthenticationToken(adminDetails,code,adminDetails.getAuthorities());
           }else {
               throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"验证码错误" );
           }
        }else {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"验证码已过期" );
        }
    }

    @Override
    public boolean supports(Class<?> authentication) {
        //return authentication.equals(UsernamePasswordAuthenticationToken.class);
        return true;
    }
}
